import style from "./style.css";

@Midway.MidwayModule({
    code: "Launcher",
    style: style,
    template: require('./view.html'),
    dependLibs: ['audio5js']
})
class DemoModule extends CommonClass {
    constructor() {
        super();
        this.objectModel = {
            Button: {
                info: "this is message from button"
            },
            Layout: {
                info: "this is message from layout"
            },
            UserInfo: {
                info: "this is message from user info"
            },
            Controller: {
                info: "this is message from controller"
            }
        };
        this.layoutInstance = undefined;
    }

    $launch() {
        this.loadAll();
    }

    loadAll() {
        this.requireComponent("Layout", [{
            name: "info",
            type: "string",
            value: "this is message from layout"
        }], ".js_layout", {}).then(layout => {
            layout.initContainer();
            this.layoutInstance = layout;
            this.loadController();
        });
    }

    loadController() {
        const promise = this.PlayerController.require({
            code: 'Controller',
            model: this.objectModel['Controller']
        });
        return promise.done(() => {
            this.loadComponent();
        });
    }

    loadComponent() {
        const components = ["UserInfo", "Button"];
        let promises = [];
        components.forEach(item => {
            const renderTo = this.layoutInstance.getContainer(item);
            const promise = this.PlayerController.require({
                code: item,
                model: this.objectModel[item],
                rendTo: renderTo
            });
            promises.push(promise);
        });
        return $.when.apply($, promises).done(() => {
            console.log("Bundle Load Complete");
        });
    }
}